package king.dominic.jlibrary.view;

import android.app.Dialog;
import android.support.annotation.IntDef;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import java.lang.annotation.Retention;

import king.dominic.jlibrary.R;

import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
 * Created by king on 2016/11/17.
 * BottomTwoChoiceDialog
 */

public class BottomTwoChoiceDialog {
    public static final int BUTTON_FIRST = 1;
    public static final int BUTTON_SECOND = 2;
    public static final int BUTTON_CANCEL = 3;
    private final AppCompatActivity context;
    private OnMenuClickListener onMenuClickListener;
    private Dialog dialog;

    public BottomTwoChoiceDialog(AppCompatActivity context, String first, String second, String cancel) {
        super();
        this.context = context;
        initView(first, second, cancel);
    }


    @Retention(SOURCE)
    @IntDef({BUTTON_FIRST, BUTTON_SECOND, BUTTON_CANCEL})
    public @interface ButtonType {

    }

    private void initView(final String first, final String second, final String cancel) {
        View view = View.inflate(context, R.layout.popup_menu, null);
        view.setMinimumWidth(WindowUtil.width);

        Button buttonFirst = (Button) view.findViewById(R.id.button_first);
        Button buttonSecond = (Button) view.findViewById(R.id.button_second);
        Button buttonCancel = (Button) view.findViewById(R.id.button_cancel);
        buttonFirst.setText(first);
        buttonSecond.setText(second);
        buttonCancel.setText(cancel);
        buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMenuClickListener==null || !onMenuClickListener.onClick(BottomTwoChoiceDialog.this, v, BUTTON_FIRST,first))
                    dismiss();
            }
        });
        buttonSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMenuClickListener==null || !onMenuClickListener.onClick(BottomTwoChoiceDialog.this, v, BUTTON_SECOND,second))
                    dismiss();
            }
        });
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMenuClickListener==null || !onMenuClickListener.onClick(BottomTwoChoiceDialog.this, v, BUTTON_CANCEL,cancel))
                    dismiss();
            }
        });

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.iOSActionSheetDialogStyle);
        dialog.setContentView(view);

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.START | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    public interface OnMenuClickListener {
        boolean onClick(BottomTwoChoiceDialog menu, View button, @ButtonType int type, String name);
    }
    public void cancel(){
        dialog.cancel();
    }
    public void dismiss(){
        dialog.dismiss();
    }
    public void show() {
        dialog.show();
    }
}
